/******************************************************************
* Panel Data Analysis with Fixed Effects, Robustness, and Year Fixed Effects
* Study: THE RELATIONSHIP BETWEEN ACCOUNTING INFORMATION QUALITY AND MARKET VALUE IN BRAZILIAN COMPANIES
* Authors: Leite, J. P. P. C., Novais, F. M. O., Rossetti, N., Carvalho, F. L.
* Date of Analysis: 2025-08-05
*
* Description:
* This script performs panel data analyses on an unbalanced panel dataset (2011-2020).
* Procedures include:
* - Panel structure declaration (xtset)
* - Descriptive statistics
* - Correlation matrices (raw and winsorized data)
* - Estimation of multiple models: OLS, Between Effects (BE), Fixed Effects (FE), Random Effects (RE)
* - Application of robust standard errors clustered by firm (Firm_id)
* - Diagnostic tests: Hausman test, Breusch-Pagan LM test, Modified Wald test for heteroskedasticity, F-test for fixed effects
* - Models including year fixed effects to control for temporal shocks
* - Alternative accounting quality proxies tested in robustness models
*
* Requirements:
* - Stata 15 or higher
* - Dataset loaded to running this script
*
* Notes:
* - Interpretations and discussions of results are available in the main manuscript.
******************************************************************/

* ---------------------------
* 1. Declare panel structure
* ---------------------------
xtset Firm_id Year

* ---------------------------
* 2. Descriptive statistics and outlier treatment
* ---------------------------
sum MB DY Capex Size ROE Indebt Growth MdfjonesTACC

sum MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w

* ---------------------------
* 3. Correlation matrices before and after winsorization
* ---------------------------
correlate MB DY Capex Size ROE Indebt Growth MdfjonesTACC
correlate MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w

* ---------------------------
* 4. OLS regressions: raw and winsorized data
* ---------------------------
reg MB DY Capex Size ROE Indebt Growth MdfjonesTACC
reg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w

* Store OLS estimates
regress MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w
estimates store ols

* Check multicollinearity with VIF
vif

* ---------------------------
* 5. OLS regression with clustered robust standard errors
* ---------------------------
reg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w, vce(cluster Firm_id)
estimates store POLS_rob

* ---------------------------
* 6. Between Effects Model
* ---------------------------
reg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w, be
estimates store BE

* ---------------------------
* 7. Fixed Effects Model (FE)
* ---------------------------
xtreg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w, fe
estimates store FE

* FE with clustered robust SE
xtreg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w, fe vce(cluster Firm_id)
estimates store FE_rob

* ---------------------------
* 8. Random Effects Model (RE)
* ---------------------------
xtreg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w, re
estimates store RE

* RE with clustered robust SE
xtreg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w, re vce(cluster Firm_id)
estimates store RE_rob

* ---------------------------
* 9. Breusch-Pagan LM Test for Random Effects
* ---------------------------
xttest0

* ---------------------------
* 10. F-Test for Fixed Effects Significance
* ---------------------------
xtreg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w, fe

* ---------------------------
* 11. Hausman Test for FE vs RE
* ---------------------------
hausman FE RE, sigmamore

* ---------------------------
* 12. Test for Heteroskedasticity in FE Model (Modified Wald test)
* ---------------------------
xttest3

* ---------------------------
* 13. FE Model with Clustered Robust SE (repeated for clarity)
* ---------------------------
xtreg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w, fe vce(cluster Firm_id)

* ---------------------------
* 14. FE Model with Year Fixed Effects and Clustered Robust SE
* ---------------------------
xtreg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w i.Year, fe vce(cluster Firm_id)
estimates store FE_rob_y

* ---------------------------
* 15. Export Estimation Results Table
* ---------------------------
esttab ols POLS_rob BE RE RE_rob FE FE_rob FE_rob_y using tabela_robustez.txt, b(4) se(4) star(* 0.1 ** 0.05 *** 0.01) ///
stats(N r2 r2_o r2_b r2_w F chi2, fmt(0 3 3 3 3 3 3)) replace

* ---------------------------
* 16. Alternative Accounting Quality Proxies Models
* ---------------------------
xtreg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w Mdf_abs_DACC i.Year, fe vce(cluster Firm_id)
estimates store Mdf_abs_DACC

xtreg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w MdfjonesTACC_w i.Year, fe vce(cluster Firm_id)
estimates store MdfjonesTACC_w

xtreg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w PAE_abs_DACC i.Year, fe vce(cluster Firm_id)
estimates store PAE_abs_DACC

xtreg MB_w DY_w Capex_w Size_w ROE_w Indebt_w Growth_w ROA_abs_DACC i.Year, fe vce(cluster Firm_id)
estimates store ROA_abs_DACC

esttab Mdf_abs_DACC MdfjonesTACC_w PAE_abs_DACC ROA_abs_DACC using tabela_modelos_qualidade.txt, b(4) se(4) star(* 0.1 ** 0.05 *** 0.01) ///
stats(N r2 r2_o r2_b r2_w F, fmt(0 3 3 3 3 3)) replace

